import { mapState } from 'vuex'
import store from "@/store"

// 尝试将用户在根目录中的store/index.js的vuex的state变量，全部加载到全局变量中
let $uStoreKey = [];
try{
	$uStoreKey = store.state ? Object.keys(store.state) : [];
}catch(e){
	
}

module.exports = {
	beforeCreate() {
		// 将vuex方法挂在到$u中
		// 使用方法为：如果要修改vuex的state中的user.name变量为"史诗" => this.$u.vuex('user.name', '史诗')
		// 如果要修改vuex的state的version变量为1.0.1 => this.$u.vuex('version', '1.0.1')
		this.$u.vuex = (name, value,isLifeData = false) => {
			let nameArr = name.toString().split('/');
			let firstLeval = [];
			let key = '';
			for(let i = 0; i< nameArr.length; i++){
				if(i == nameArr.length-1){
					key =  nameArr[i].toString(); 
				}else{
					firstLeval.push(nameArr[i].toString());
				}
				
			}
			firstLeval = firstLeval.join('/');
			this.$store.commit(`${firstLeval}/$uStore`, {
				name: key,value,isLifeData
			})
		}
		this.$u.vuexGet = (name) => { /*  此方法是新增的获取值得方法 */
			let nameArr = name.toString().split('/');
			let firstLeval = [];
			let key = '';
			for(let i = 0; i< nameArr.length; i++){
				if(i == nameArr.length-1){
					key =  nameArr[i].toString(); 
				}else{
					firstLeval.push(nameArr[i].toString());
				}
			}
			firstLeval = firstLeval.join('/');
			return this.$store.getters[`${firstLeval}/$uStoreGet`](key);
		}
	},
	computed: {
		// 将vuex的state中的所有变量，解构到全局混入的mixin中
		...mapState($uStoreKey)
	}
}